import { login, syncMiniUserInfo, getSign } from '../../api/index'
Page({
  data: {
    filename: '64aac9e1b91698976.png',
    avatarUrl: 'https://inspiredealer.oss-cn-beijing.aliyuncs.com/EY/knowledgePlatform/64aac9e1b91698976.png'
  },
  onLoad() {},
  login() {
    const that = this
    const { filename, nickName } = that.data
    // if (!filename.length) return wx.showToast({ title: '请上传您的头像', icon: 'none'})
    if (!nickName) return wx.showToast({ title: '请填写您的昵称', icon: 'none'})
    wx.showLoading({title: '登录中'})
    wx.login({
      success (res) {
        if (res.code) {
          login({ code: res.code }).then(res => {
            wx.setStorageSync('token', res.data.token)
            syncMiniUserInfo({ avatarUrl: filename, nickName: nickName }).then(res => {
              wx.hideLoading()
              wx.showToast({ title: '登录成功', icon: 'none'})
              wx.reLaunch({url: '/pages/index/index'})
              
            }).catch(err => {
              console.log(err)
              wx.hideLoading()
              wx.showToast({ title: '登录成功', icon: 'none'})
              wx.reLaunch({url: '/pages/index/index'})
            })
          }).catch(err => {
            wx.hideLoading()
            wx.showToast({
              title: err.errorMsg,
              icon: 'none',
            })
          })
        } else {
          console.log(err)
          wx.showToast({
            title: '登录失败',
            icon: 'none'
          })
          wx.hideLoading()
        }
      },
      fail (err) {
        console.log(err)
        wx.showToast({
          title: '登录失败',
          icon: 'none'
        })
        wx.hideLoading()
      }
    })
  },
  onShareAppMessage: function () {
    // 页面被用户分享时执行
    const promise = new Promise(resolve => {
      setTimeout(() => {
        resolve({
          title: 'EY汽车知识库，享你所想！'
        })
      }, 2000)
    })
    return {
      title: 'EY汽车知识库',
      path: '/page/index/index',
      promise 
    }
  },
  // 头像上传
  onChooseAvatar(e) {
    const that = this
    const { avatarUrl } = e.detail
    wx.login({
      success (res) {
        if (res.code) {
          wx.showLoading({ title: '',mask: true })
          login({ code: res.code }).then(res => {
            const token  = res.data.token
            wx.setStorageSync('token', token)
            getSign().then(res => {
              let params = res.data
              params.file = avatarUrl
              
              let filename = `${token.substring(0, 10)}${(new Date()).getTime().toString().substring(0, 7)}.png`
              const key = `${(params.dir)}${filename}`
              console.log(filename)
              let data = {}
              data['key'] = key
              data['policy'] = params.policy
              data['OSSAccessKeyId'] = params.accessId
              data['success_action_status'] = 200
              data['signature'] = params.signature
              data['name'] = filename
              console.log(data)
              wx.uploadFile({
                url: params.host, //仅为示例，非真实的接口地址
                filePath:params.file,
                name: 'file',
                formData: data,
                header: { 'Content-Type': 'multipart/form-data' },
                success (res){
                  wx.hideLoading()
                  if (res.statusCode === 200) {
                    that.setData({ avatarUrl, filename })
                    wx.removeStorageSync('token')
                  } else {
                    wx.removeStorageSync('token')
                    console.log(res)
                    wx.showToast({
                      title: '上传失败',
                      icon: 'none'
                    })
                  }
                },
                fail (err) {
                  wx.hideLoading()
                  wx.removeStorageSync('token')
                  console.log(err)
                  wx.showToast({
                    title: '上传失败',
                    icon: 'none'
                  })
                }
              })
            }).catch(err => {
              wx.hideLoading()
              wx.removeStorageSync('token')
              console.log(err)
              wx.showToast({
                title: '上传失败',
                icon: 'none'
              })
            })
          }).catch(err => {
            wx.hideLoading()
            wx.removeStorageSync('token')
            console.log(err)
            wx.showToast({
              title: '上传失败',
              icon: 'none'
            })
          })
        } else {
          console.log(res)
          wx.hideLoading()
          wx.removeStorageSync('token')
          wx.showToast({
            title: '上传失败',
            icon: 'none'
          })
        }
      },
      fail (err) {
        wx.removeStorageSync('token')
        console.log(err)
        wx.showToast({
          title: '上传失败',
          icon: 'none'
        })
      }
    })
  },
  // 昵称上传
  changeg(e) {
    this.setData({
      nickName: e.detail.value
    })
  }
})
